package com.wei.czz.framework.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.enums.ResultEnum;
import com.wei.czz.common.exception.CzzException;
import com.wei.czz.common.po.UserPo;
import com.wei.czz.common.utils.SecurityUtils;
import com.wei.czz.framework.admin.dao.DeptRoleDao;
import com.wei.czz.framework.admin.entity.DeptRoleEntity;
import com.wei.czz.framework.admin.service.DeptRoleService;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service("deptRoleService")
@AllArgsConstructor
public class DeptRoleServiceImpl extends ServiceImpl<DeptRoleDao, DeptRoleEntity> implements DeptRoleService {

    private static final Logger log = LoggerFactory.getLogger(DeptRoleServiceImpl.class);

    @Override
    public void saveOrEdit(DeptRoleEntity deptRole) {
        if (Objects.isNull(deptRole.getId())) {
            int count = baseMapper.insert(deptRole);
            log.info("保存部门与角色关联关系完成。count={}", count);
        } else {
            int count = baseMapper.updateById(deptRole);
            log.info("修改部门与角色关联关系完成。count={}", count);
        }
    }

    @Override
    public DeptRoleEntity get(Long id) {
        // 查询
        DeptRoleEntity deptRole = baseMapper.selectById(id);
        if (Objects.isNull(deptRole)) {
            log.info("部门与角色关联对象不存在");
            throw new CzzException(ResultEnum.ILLEGAL_FAIL);
        }
        if (CommonEnum.ONE.getValue().equals(deptRole.getDeleteStatus())) {
            log.info("部门与角色关联对象已删除");
            throw new CzzException(ResultEnum.NOT_FOUND);
        }
        return deptRole;
    }

    @Override
    public List<DeptRoleEntity> getList(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            log.info("获取部门与角色关联操作，传递的主键列表为空");
            return Collections.emptyList();
        }

        LambdaQueryWrapper<DeptRoleEntity> deptRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        deptRoleLambdaQueryWrapper.in(DeptRoleEntity::getId, idList)
                .eq(DeptRoleEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 查询
        List<DeptRoleEntity> deptRoleList = baseMapper.selectList(deptRoleLambdaQueryWrapper);
        log.info("查询部门与角色关联数据完成。list.size={}", deptRoleList.size());
        return deptRoleList;
    }

    @Override
    public DeptRoleEntity findDeptRole(Long id) {
        DeptRoleEntity deptRole = baseMapper.selectById(id);
        if (Objects.isNull(deptRole)) {
            log.info("部门与角色关联关系不存在。id={}", id);
            throw new CzzException(ResultEnum.REQUEST_FAIL.getCode(), "部门与角色关联关系不存在");
        }
        if (CommonEnum.ONE.getValue().equals(deptRole.getDeleteStatus())) {
            log.info("部门与角色关联关系已删除。id={}", id);
            throw new CzzException(ResultEnum.REQUEST_FAIL.getCode(), "部门与角色关联关系不存在");
        }
        return deptRole;
    }

    @Override
    public List<DeptRoleEntity> findDeptRoleList(List<Long> deptIdList) {
        if (CollectionUtils.isEmpty(deptIdList)) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<DeptRoleEntity> deptRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        deptRoleLambdaQueryWrapper.in(DeptRoleEntity::getDeptId, deptIdList)
                .eq(DeptRoleEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 查询
        List<DeptRoleEntity> deptRoleList = baseMapper.selectList(deptRoleLambdaQueryWrapper);
        log.info("查询部门关联角色完成。size={}", deptRoleList.size());
        return deptRoleList;
    }

    @Override
    public List<DeptRoleEntity> findList(List<Long> deptIdList, List<Long> roleIdList) {
        if (CollectionUtils.isEmpty(deptIdList) && CollectionUtils.isEmpty(roleIdList)) {
            log.info("获取部门关联角色操作，传入的部门主键列表和角色主键列表均为空");
            return Collections.emptyList();
        }

        LambdaQueryWrapper<DeptRoleEntity> deptRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        deptRoleLambdaQueryWrapper.in(!CollectionUtils.isEmpty(deptIdList), DeptRoleEntity::getDeptId, deptIdList)
                .in(!CollectionUtils.isEmpty(roleIdList), DeptRoleEntity::getRoleId, roleIdList)
                .eq(DeptRoleEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 查询
        List<DeptRoleEntity> deptRoleList = baseMapper.selectList(deptRoleLambdaQueryWrapper);
        log.info("查询部门关联角色完成。list.size={}", deptRoleList.size());
        return deptRoleList;
    }

    @Transactional
    @Override
    public void batchUpdate(List<DeptRoleEntity> deptRoleList) {
        boolean bool = this.updateBatchById(deptRoleList);
        log.info("批量更新部门与角色完成。bool={}", bool);
    }

    @Override
    public void deleteDeptRole(List<Long> idList, List<Long> deptIdList, List<Long> roleIdList) {
        if (CollectionUtils.isEmpty(idList) && CollectionUtils.isEmpty(deptIdList)
                && CollectionUtils.isEmpty(roleIdList)) {
            log.info("删除部门关联角色传递的主键列表、部门主键列表、角色主键列表均为空");
            return;
        }
        // 获取操作用户
        UserPo userPo = SecurityUtils.getUser();
        Date date = new Date();

        LambdaUpdateWrapper<DeptRoleEntity> deptRoleLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        // 设置字段值
        deptRoleLambdaUpdateWrapper.set(DeptRoleEntity::getDeleteStatus, CommonEnum.ONE.getValue())
                .set(DeptRoleEntity::getUpdateTime, date)
                .set(DeptRoleEntity::getUpdateUser, userPo.getUsername())
                .set(DeptRoleEntity::getUpdateUserId, userPo.getUserId());
        deptRoleLambdaUpdateWrapper.in(!CollectionUtils.isEmpty(idList), DeptRoleEntity::getId, idList)
                .in(!CollectionUtils.isEmpty(deptIdList), DeptRoleEntity::getDeptId, deptIdList)
                .in(!CollectionUtils.isEmpty(roleIdList), DeptRoleEntity::getRoleId, roleIdList)
                .eq(DeptRoleEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 删除
        int count = baseMapper.update(deptRoleLambdaUpdateWrapper);
        log.info("批量删除部门与角色关联关系完成。count={}", count);
    }

}